package com.yiwenup.leetcode.site;

import com.yiwenup.leetcode.ListNode;

/**
 * 文件描述
 *
 * @Project struct-algorithm
 * @Package com.yiwenup.leetcode.site
 * @Author yiwenup
 * @Date 2024-12-11 21:53:53
 * @Description https://leetcode.cn/problems/remove-linked-list-elements/description/
 */
public class No0203 {
    /**
     * 虚拟头节点方式 dummy -> 0 -> 1 -> 2
     */
    public ListNode removeElements(ListNode head, int val) {
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;
        ListNode curr = dummyHead;
        while(curr != null && curr.next != null) {
            if(curr.next.val == val) {
                curr.next = curr.next.next;
            } else {
                curr = curr.next;
            }
        }
        return dummyHead.next;
    }

    /**
     * 原始处理方式
     */
    public ListNode removeElements2(ListNode head, int val) {
        while(head != null && head.val == val) {
            head = head.next;
        }
        ListNode curr = head;
        while(curr != null && curr.next != null) {
            if(curr.next.val == val) {
                curr.next = curr.next.next;
            } else {
                curr = curr.next;
            }
        }
        return head;
    }
}
